<?php
namespace Home\Model;
use  Think\Model;
class GoodsBaseModel extends Model{
protected $autoCheckFields =false;

  /**
     * 获得指定商品的相册
     *
     * @access  public
     * @param   integer     $goods_id
     * @return  array
     */
    function get_goods_gallery($goods_id) {
       
         //商品详情页相册图片
        $number=M('shop_config')->where("code='goods_gallery_number'")->getField('value');

        $goods_gallery_number=$number?$number:10;


        $sql = 'SELECT img_id, img_url, thumb_url, img_desc' .
                ' FROM ' . C('DB_PREFIX') .
                "goods_gallery WHERE goods_id = '$goods_id' order by img_desc desc LIMIT " . $goods_gallery_number;

        $row = M()->query($sql);

        $new_row=array();
        /* 格式化相册图片路径 */
        foreach ($row as $key => $gallery_img) {
            $new_row[$key]['img_url'] = get_image_path($gallery_img['img_url']);
            // $row[$key]['thumb_url'] = get_image_path($gallery_img['thumb_url']);
            // $row[$key]['img_desc'] = $gallery_img['img_desc'];
        }
        return $new_row;
    }

   /**
     * 取得商品优惠价格列表
     *
     * @param   string  $goods_id    商品编号
     * @param   string  $price_type  价格类别(0为全店优惠比率，1为商品优惠价格，2为分类优惠比率)
     *
     * @return  优惠价格列表
     */
   public  function get_volume_price_list($goods_id, $price_type = '1') {

       $DB_PREFIX=C('DB_PREFIX');

        $volume_price = array();
        $temp_index = '0';

        $sql = "SELECT `volume_number` , `volume_price`" .
                " FROM " . $DB_PREFIX . "" .
                "volume_price WHERE `goods_id` = '" . $goods_id . "' AND `price_type` = '" . $price_type . "'" .
                " ORDER BY `volume_number`";

        $res = M()->query($sql);
        if(!$res)return array();

        foreach ($res as $k => $v) {
            $volume_price[$temp_index] = array();
            $volume_price[$temp_index]['number'] = $v['volume_number'];
            $volume_price[$temp_index]['price'] = $v['volume_price'];
            $volume_price[$temp_index]['format_price'] = price_format($v['volume_price']);
            $temp_index++;
        }
        return $volume_price;
    }
      
   /**
     * 取得商品最终使用价格
     *
     * @param   string  $goods_id      商品编号
     * @param   string  $goods_num     购买数量
     * @param   boolean $is_spec_price 是否加入规格价格
     * @param   mix     $spec          规格ID的数组或者逗号分隔的字符串
     *
     * @return  商品最终购买价格
     */
    public function get_final_price($session,$goods_id, $goods_num = '1', $is_spec_price = false, $spec = array()) {
        $final_price = '0'; //商品最终购买价格
        $volume_price = '0'; //商品优惠价格
        $promote_price = '0'; //商品促销价格
        $user_price = '0'; //商品会员价格
        //取得商品优惠价格列表
        $price_list = $this->get_volume_price_list($goods_id, '1');

        $DB_PREFIX=C('DB_PREFIX');


        if (!empty($price_list)) {
            foreach ($price_list as $value) {
                if ($goods_num >= $value['number']) {
                    $volume_price = $value['price'];
                }
            }
        } 


        //取得商品促销价格列表
        /* 取得商品信息 */
        $sql = "SELECT g.promote_price, g.promote_start_date, g.promote_end_date, " .
                "IFNULL(mp.user_price, g.shop_price * '" . $session['discount'] . "') AS shop_price " .
                " FROM " .$DB_PREFIX . "goods AS g " .
                " LEFT JOIN " . $DB_PREFIX . "member_price AS mp " .
                "ON mp.goods_id = g.goods_id AND mp.user_rank = '" . $session['user_rank'] . "' " .
                " WHERE g.goods_id = '" . $goods_id . "'" .
                " AND g.is_delete = 0";
        $ar_goods = M()->query($sql); 

        $goods=$ar_goods[0];



        

        /* 计算商品的促销价格 */
        if ($goods['promote_price'] > 0) {
            $promote_price = bargain_price($goods['promote_price'], $goods['promote_start_date'], $goods['promote_end_date']);
        } else {
            $promote_price = 0;
        }

        //取得商品会员价格列表
        $user_price = $goods['shop_price'];

        //比较商品的促销价格，会员价格，优惠价格
        if (empty($volume_price) && empty($promote_price)) {
            //如果优惠价格，促销价格都为空则取会员价格
            $final_price = $user_price;
        } elseif (!empty($volume_price) && empty($promote_price)) {
            //如果优惠价格为空时不参加这个比较。
            $final_price = min($volume_price, $user_price);
        } elseif (empty($volume_price) && !empty($promote_price)) {
            //如果促销价格为空时不参加这个比较。
            $final_price = min($promote_price, $user_price);
        } elseif (!empty($volume_price) && !empty($promote_price)) {
            //取促销价格，会员价格，优惠价格最小值
            $final_price = min($volume_price, $promote_price, $user_price);
        } else {
            $final_price = $user_price;
        }


        //如果需要加入规格价格
        if ($is_spec_price) {
            if (!empty($spec)) {
                $spec_price = D('Goods')->spec_price($spec);
                $final_price += $spec_price;
            }
        }
        //返回商品最终购买价格
        return $final_price;
    }

	 /**
     *
     * 是否存在规格
     *
     * @access      public
     * @param       array       $goods_attr_id_array        一维数组
     *
     * @return      string
     */
    function is_spec($goods_attr_id_array, $sort = 'asc') {
        if (empty($goods_attr_id_array)) {
            return $goods_attr_id_array;
        }

        //重新排序
        $sql = "SELECT a.attr_type, v.attr_value, v.goods_attr_id
            FROM " . C('DB_PREFIX') . "attribute AS a
            LEFT JOIN " . C('DB_PREFIX') . "goods_attr AS v
                ON v.attr_id = a.attr_id
                AND a.attr_type = 1
            WHERE v.goods_attr_id " . db_create_in($goods_attr_id_array) . "
            ORDER BY a.attr_id $sort";

        $row = M()->query($sql);
        $return_arr = array();
        foreach ($row as $value) {
            $return_arr['sort'][] = $value['goods_attr_id'];
            $return_arr['row'][$value['goods_attr_id']] = $value;
        }
        if (!empty($return_arr)) {
            return true;
        } else {
            return false;
        }
    }

     /**
     *
     * 是否存在一个商品的规格
     *
     * @access      public
     * @param       array       $goods_attr_id_array        一维数组
     *
     * @return      string
     */
    function is_goods_spec($goods_attr_id_array, $sort = 'asc') {
        if (empty($goods_attr_id_array)) {
            return $goods_attr_id_array;
        }

        //重新排序
        $sql = "SELECT v.goods_id
            FROM " . C('DB_PREFIX') . "attribute AS a
            LEFT JOIN " . C('DB_PREFIX') . "goods_attr AS v
                ON v.attr_id = a.attr_id
                AND a.attr_type = 1
            WHERE v.goods_attr_id " . db_create_in($goods_attr_id_array) . "
            ORDER BY a.attr_id $sort";


        $row = M()->query($sql);
        $return_arr = array();
        $goods_id=0;
        $diff=0;


        foreach ($row as $value) {
              if(!$goods_id)
              {
                $goods_id=$value['goods_id'];
              }
              else if($goods_id!==$value['goods_id'])
                {$diff++;}
              
        }    
        if (!$diff) {
            return $goods_id;
        } else {
            return false;
        }
    }

        /**
     * 调用某商品的累积收藏
     * @param type $arr
     * @return int
     */
    public function get_goods_collect($goods_id = 0) {
        $sql = "SELECT count(*) as count FROM " . C('DB_PREFIX') .
                "collect_goods WHERE goods_id = '" . $goods_id . "'";
        $count = M()->query($sql);
        return $count['count'];
    }

}
?>
